<?php
namespace Article\Controller;

class AdminController extends \Common\Controller\BaseController {

	public $article,$category;

	public function _initialize(){
		parent::_initialize();
		$this->article = D('Article');
		$this->category = D('ArticleCate');
		$top_nav = array(
				array('name'=>'资讯列表','url'=>U('Article/Admin/index'),'tab'=>array('index','add','eidt')),
				array('name'=>'类别列表','url'=>U('Article/Admin/type_list'),'tab'=>array('type_list','type_edit','type_add')),
				array('name'=>'添加资讯','url'=>U('Article/Admin/add'),'right'=>1),
				array('name'=>'添加类别','url'=>U('Article/Admin/type_add'),'right'=>1),
		);
		$this->assign('top_nav',$top_nav);
	}

	//资讯列表
	public function index(){
		$map = array();
		if(I('keyword')){
			$map['title'] = array('LIKE','%'.I('keyword').'%');
		}
                if(I('parentid')){
                    $map['cateid']=I('parentid');
                }
		$count = $this->article->where($map)->count();
		$page = new \Com\Page($count,15);

		$limit = $page->firstRow.",".$page->listRows;
		$order = "articleid desc";

		$list = $this->article->where($map)->order($order)->limit($limit)->select();
		$data = array(
			'list'   => $list,
			'keyword' => I('keyword'),
			'page'   => $page->show(),
		);
		$this->assign($data);
		// $option_tree = $this->getCategory(true,0);
		// $this->assign('option_tree',$option_tree);
		$this->display();
	}

	//资讯添加
	public function add(){
		if (IS_POST) {
			$data = $this->article->create();
			if ($data) {
				$result = $this->article->add();
				if ($result) {
					$this->success("添加成功！",U('Article/Admin/index'));
				}else{
					$this->error($this->article->getError());
				}
			}else{
				$this->error($this->article->getError());
			}
		}else{
			$option_tree = $this->getCategory(true,0);
			$this->assign('option_tree',$option_tree);
			$this->display();
		}
	}

	//资讯编辑
	public function edit(){
		if (IS_POST) {
			$data = $this->article->create();
			if ($data) {
				$result = $this->article->save();
				if ($result) {
					$this->success("修改成功！",U('Article/Admin/index'));
				}else{
					$this->error("修改失败！");
				}
			}else{
				$this->error($this->article->getError());
			}
		}else{
			$id = I('id','','intval');
			if ($id == '') {
				$this->error("非法操作！");
			}
			$data = $this->article->where(array(
				'articleid'   => $id,
			))->find();
			$option_tree = $this->getCategory(true,$data['cateid']);
			$this->assign('option_tree',$option_tree);
			$this->assign($data);
			$this->display('add');
		}
	}

	//资讯删除
	public function del(){
		$id = I('get.id','','intval');
		if ($id == '') {
			$this->error("非法操作！");
		}
		$result = $this->article->where(array(
			'articleid'  => $id,
		))->delete();
		if ($result) {
			$this->success("删除成功！");
		}else{
			$this->error("删除失败！");
		}
	}

	//资讯操作
	public function todo(){
		$id = I('get.id','','intval');
		$isrecommend = I('get.isrecommend','','intval');
		if ($id == '') {
			$this->error("非法操作！");
		}
		$result = $this->article->where(array(
			'articleid'  => $id,
		))->find();
		if ($result) {
			//推荐操作
			$result = $this->article->where(array(
				'articleid'  => $id,
			))->save(array(
				'isrecommend' => $isrecommend,
			));
			if ($result) {
				$this->success("操作成功！");
			}else{
				$this->error($this->article->getError());
			}
		}else{
			$this->error("操作失败！");
		}
	}

	//资讯类别列表
	public function type_list(){
		$wherearr = array();
		$field = "cateid,parentid,catename";
		$list = $this->category->getListByMap($wherearr,$field,$limit,$order);
		$array = array();
		// 构建生成树中所需的数据
		foreach($list as $k => $r) {
			$r['id']      = $r['cateid'];
			$r['pid']      = $r['parentid'];
			$r['add_children'] = "<a href='".U('Article/Admin/type_add',array('id'=>$r['cateid']))."' class='btn btn-sm btn-primary'>添加子栏目</a>";
			$r['edit'] = "<a href='".U('Article/Admin/type_edit',array('id'=>$r['cateid']))."' class='btn btn-sm btn-primary'>编辑</a>";
			$r['delete'] = "<a href=\"".U('Article/Admin/type_del',array('id'=>$r['cateid']))."\" class='btn btn-sm btn-danger'>删除</a>";
			$array[]      = $r;
		}
		
		$str  = "<tr>
		  <td width='70'><label class='checkbox'><input type='checkbox' name='id' value='\$id'>\$id</label></td>
		  <td>\$spacer \$catename</td>
		  <td width='300'>
			<div class='btn-group'>\$add_content \$add_children \$edit \$delete</div>
		  </td>
		</tr>";
		
  		$Tree = new \Com\Tree();
		$Tree->icon = array('&nbsp;&nbsp;│ ','&nbsp;&nbsp;├─ ','&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;';
		$Tree->init($array);
		$html_tree = $Tree->get_tree(0, $str);
		$this->assign('html_tree',$html_tree);
		$this->display();
	}

	//资讯类别添加
	public function type_add(){
		if (IS_POST) {
			$data = $this->category->create();
			if ($data) {
				$result = $this->category->add();
				if ($result) {
					$this->success("添加成功！",U('Article/Admin/type_list'));
				}else{
					$this->error($this->category->getError());
				}
			}else{
				$this->error($this->category->getError());
			}
		}else{
			$pid = I('get.id','','trim');
			$option_tree = $this->getCategory(true,$pid);
			$this->assign('option_tree',$option_tree);
			$data = array(
				'pid'=>$pid,
			);
			$this->assign($data);
			$this->display();
		}
	}

	//资讯类别编辑
	public function type_edit(){
		if (IS_POST) {
			$data = $this->category->create();
			if ($data) {
				$result = $this->category->save();
				if ($result) {
					$this->success("编辑成功！",U('Article/Admin/type_list'));
				}else{
					$this->error("无更新……");
				}
			}else{
				$this->error($this->category->getError());
			}
		}else{
			$id = I('id');
			if ($id == '') {
				$this->error("非法操作！");
			}
			$data = $this->category->where(array(
				'cateid'  => $id,
			))->find();
			$option_tree = $this->getCategory(true,$data['parentid']);
			$this->assign('option_tree',$option_tree);
			$this->assign($data);
			$this->display('Admin:type_add');
		}
	}

	//资讯类别删除
	public function type_del(){
		$id = I('get.id','','intval');
		if ($id == '') {
			$this->error("非法操作！");
		}
		$result = $this->category->where(array(
			'cateid'  => $id,
		))->delete();
		if ($result) {
			$this->success("删除成功！");
		}else{
			$this->error("删除失败！");
		}
	}

	public function getCategory($tree=false,$pid){
		$list = $this->category->getCateList();
		foreach($list as $val){
			$data[$val['cateid']] = $val;
		}
		if($tree){
			$array = array();
			foreach($data as $r) {
				$r['id'] = $r['cateid'];
				$r['pid'] = $r['parentid'];
				$array[] = $r;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$catename</option>";
			$tree = new \Com\Tree();
			$tree->icon = array('&nbsp;&nbsp;│ ','&nbsp;&nbsp;├─ ','&nbsp;&nbsp;└─ ');
			$tree->nbsp = '&nbsp;&nbsp;';
			$tree->init($array);
			return $tree->get_tree(0, $str,$pid);	
		}else{
			return $data;
		}
	}
}